package com.dbw.pdftool;

public class Main {

	private static void usage() {
		System.out.println("usage:");
		System.out.println("bktool action param");
		System.out.println("examples:");
		System.out.println("bktool make_bk input_pdf input_bookmark_file [input_pattern_file]");
		System.out.println("bktool make_bk_desc [bookmark_desc_file]");
		System.out.println("bktool auto_bk input_pdf bookmark_demo_file");
		System.out.println("bktool auto_bk_demo [bookmark_demo_file]");
		System.out.println("bktool optimise input_pdf");
	}
	
	private static String makeNewPath(String path) {
		int index = path.lastIndexOf('.');
		if(index == -1) {
			return path + "_new";
		}
		
		return path.substring(0, index) + "-new" + path.substring(index);
	}
	
	public static void main(String[] args) {
		
		try {
			if(args.length < 1) {
				throw new IllegalArgumentException("need input action");
			}
			if(args[0].equals("make_bk")) {
				if(args.length != 3 && args.length != 4) {
					throw new IllegalArgumentException("illegal parameter");
				} 
				SimpleBookmarkMaker.makeBookmark(args[1], args[2], 
						args.length > 3 ? args[3] : null, 
						makeNewPath(args[1]));
			} else if(args[0].equals("make_bk_desc")) {
				if(args.length != 1 && args.length != 2) {
					throw new IllegalArgumentException("illegal parameter");
				}
				SimpleBookmarkMaker.makeStandardBookmarkDescription(args.length > 1 ? args[1] : null);
			} else if(args[0].equals("auto_bk")) {
				if(args.length != 3) {
					throw new IllegalArgumentException("illegal parameter");
				}
				AutoBookmarkMaker.makeBookmark(args[1], args[2], makeNewPath(args[1]));
			} else if(args[0].equals("auto_bk_demo")) {
				if(args.length != 1 && args.length != 2) {
					throw new IllegalArgumentException("illegal parameter");
				}
				AutoBookmarkMaker.makeBookmarkDemoFile(args.length > 1 ? args[1] : null);
			} else if(args[0].equals("optimise")) {
                if(args.length != 2) {
                    throw new IllegalArgumentException("illegal parameter");
                }
                BookmarkOptimiser.optimise(args[1]);
            } else {
				throw new IllegalArgumentException("unknown action");
			} 
			    
			
		} catch (IllegalArgumentException e) {
			usage();
		}
	}
}
